
import json
def save(d,name):
    f=open(name,"w")
    d2=[x.__dict__ for x in d]
    json.dump(d2,f,ensure_ascii=False)
    f.close()
def read(name):
    d={}
    f = open(name, "r")
    d=json.load(f)
    d=[ Stu(**x) for x in d]
    #Stu(name=xx,age=xx,phone=xx)
    return d
class Stu:
    def __init__(self,name,age,phone):
        self.name=name
        self.age=age
        self.phone=phone
    def __str__(self):
        return f"{self.name},{self.age},{self.phone}"
if __name__ == '__main__':
    l=[]
    '''
    [{"name":"劳务","age":21,"phone":"13928838"},{},{}]
    '''
    # l.append(Stu("老吴",21,"1399393333"))
    # l.append(Stu("xiaoma",22,"13555555"))
    # l.append(Stu("老宋",23,"13555555"))
    # save(l,"emp.json")
    #对象转换成字典格式
    #python的每个对象都有一个属性 __dict__

    l=read("emp.json")
    for s in l:
        print(type(s),s)
